from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import json
engine = create_engine('sqlite:///DataBase/DataRes/TreeJson.db', echo=True)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session= Session()

class TreeJson(Base):
    __tablename__ = 'tree_json'
    id = Column(String, primary_key=True)
    tree_json_path = Column(String)
    # url = Column(String)
    def __repr__(self):
        return "<TreeJson(name='%s', path='%s')>" % (self.name, self.path)
Base.metadata.create_all(engine)

def addTreeJson(id,time,path=r"DataBase/TreeJson/"):
    new_tree_json = TreeJson(id=id, tree_json_path=path+str(time)+".json")
    session.add(new_tree_json)
    session.commit()

def showAllTreeJson():
    for tree_json in session.query(TreeJson).all():
        print("id:"+tree_json.id+"\npath:"+tree_json.tree_json_path)

def getTreeJsonPath(id):
    tree_json = session.query(TreeJson).filter_by(id=id).first()
    return tree_json.tree_json_path

def GetKeywords(tree_json):
    keywords_combine=""
    if "key_words" in tree_json:
        keywords_combine+=tree_json["key_words"]
    if "children" in tree_json:
        for child in tree_json["children"]:
            keywords_combine+=GetKeywords(child)
    return keywords_combine
def GetExtractInTree(id):
    tree = session.query(TreeJson).filter_by(id=id).first()
    with open(tree.tree_json_path, "r", encoding="utf-8") as f:
        tree_json=f.read()
        print(tree_json)
    return GetKeywords(json.loads(tree_json))

def updateTreeJsonPath(id, new_path):
    # 查询要更新的TreeJson对象
    tree_json = session.query(TreeJson).filter_by(id=id).first()

    if tree_json:
        # 更新tree_json_path属性
        tree_json.tree_json_path = new_path
        # 提交更改
        session.commit()
        print(f"TreeJson with id={id} has been updated with a new path: {new_path}")
    else:
        print(f"TreeJson with id={id} not found. No updates were made.")




if __name__ == '__main__':
    # id='2023102700001'
    # path='DataBase/tree_json/'+id+'.json'
    # url='https://www.bilibili.com/video/BV1hR4y1Y7XF/?spm_id_from=333.337.search-card.all.click&vd_source=c0ef71e4f59a6528f8d2ce25c2774f90'
    # addTreeJson(id,path,url)
    # updateTreeJsonPath("https://rl.shuishan.net.cn/e3504f4984e04257a674b97850d6a96a/9e78e682800244e3ba3c6bd58cd73d3b-b5d5e252fc30a661b896252285f0d6f0-ld.mp4","DataBase/TreeJson/20231107183456.json")
    #showAllTreeJson()
    # path=getTreeJsonPath("https://rl.shuishan.net.cn/9134af83171d41fa837791c8fe6a85f3/57e22d0044a042e385b6c04a03563925-394db62c9d59e6a5b0f543256e26bf93-ld.mp4")
    # print(path)
    # string=GetExtractInTree("https://rl.shuishan.net.cn/9134af83171d41fa837791c8fe6a85f3/57e22d0044a042e385b6c04a03563925-394db62c9d59e6a5b0f543256e26bf93-ld.mp4")
    # print(string)
    #print(getTreeJsonPath("https://rl.shuishan.net.cn/9134af83171d41fa837791c8fe6a85f3/57e22d0044a042e385b6c04a03563925-394db62c9d59e6a5b0f543256e26bf93-ld.mp4"))
    #GetExtractInTree("https://rl.shuishan.net.cn/9134af83171d41fa837791c8fe6a85f3/57e22d0044a042e385b6c04a03563925-394db62c9d59e6a5b0f543256e26bf93-ld.mp4")
    print(GetExtractInTree("https://rl.shuishan.net.cn/e3504f4984e04257a674b97850d6a96a/9e78e682800244e3ba3c6bd58cd73d3b-b5d5e252fc30a661b896252285f0d6f0-ld.mp4"))